package com.mca.framework.stream;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class DemoReduceTest {
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);
        numbers.add(6);
        numbers.add(7);
        numbers.add(700);
        numbers.add(-7);
        //相加
//        Integer reduce = numbers.stream().reduce(0, (a, b) -> a + b);
//        System.out.println(reduce);
        //相加
//          Integer reduce = numbers.stream().reduce(0, Integer::sum);
//          System.out.println(reduce);
        Optional<Integer> optional = numbers.stream().reduce(Integer::sum);
        if(optional.isPresent()){
            Integer sum = optional.get();
            System.out.println(sum);
        }
        //获取最大值
        Optional<Integer> maxOption = numbers.stream().reduce(Integer::max);
        if(maxOption.isPresent()){
            System.out.println(maxOption.get());
        }
        //获取最小值
        Optional<Integer> min = numbers.stream().min(Integer::compareTo);
        System.out.println(min.get());
    }
}
